﻿@using eLoan.Models;
@model CollectionReconciliationAddModel

@{
    ViewBag.Title = "Add Collection Reconciliation";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Add Collection Reconciliation</h2>
        <ol class="breadcrumb">
            <li>
                Workspace
            </li>
            <li class="active">
                <strong>Add Collection Reconciliation</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("CollectionReconciliationAdd", "Workspace", FormMethod.Post, new { id = "saveForm" }))
            {
                <fieldset>
                    <div class="ibox-content">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Branch</th>
                                    <th>Collection Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        @Html.DisplayFor(m => m.SelectedBranchName)
                                        @Html.HiddenFor(m => m.SelectedBranchCode)
                                        @Html.HiddenFor(m => m.SelectedBranchName)
                                    </td>
                                    <td>
                                        @Html.ValueFor(m => m.CollectionDate, "{0:MMM/dd/yyyy}")
                                        @Html.HiddenFor(m => m.CollectionDate)
                                    </td>
                                </tr>
                            </tbody>
                        </table>       
                        <table id="tbl" class="table table-bordered table-List">
                            <thead>
                                <tr>
                                    <th>Agent</th>
                                    <th style="width:150px">Total Collection Per Record</th>
                                    <th style="width:150px">Cash Deposited</th>
                                    <th style="width:150px">Deducted To Cheque - Advance Payment</th>
                                    <th style="width:150px">Deducted To Cheque - Savings</th>
                                    <th style="width:150px">Used For Loan Release</th>
                                    <th style="width:150px">Used In Withdrawal Of Savings</th>
                                    <th style="width:150px">Others</th>
                                    <th>Remarks</th>
                                    <th style="width:150px">Total</th>
                                    <th style="width:150px">Difference</th>
                                </tr>
                            </thead>
                            <tbody id="tableBody">
                            @for (var i = 0; i < Model.CollectionReconciliationItems.Count(); i++)
                            {
                                <tr>
                                    <td>
                                        @Html.ValueFor(m => m.CollectionReconciliationItems[i].AgentName)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].AgentName)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].AgentID)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].Date)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].BranchCode)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].BranchName)
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.ValueFor(m => m.CollectionReconciliationItems[i].TotalCollectionPerRecord, "{0:#,###,##0.00}")
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].TotalCollectionPerRecord, new { @id = "hdnTotalPerRecord" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].CashDeposited, "{0:#,###,##0.00}", new { @id = "cashDeposited_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i +")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].DeductedToChequeAdvancePayment, "{0:#,###,##0.00}", new { @id = "deductedToChequeAdvancePayment_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i + ")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].DeductedToChequeSavings, "{0:#,###,##0.00}", new { @id = "deductedToChequeSavings_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i + ")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].UsedForLoanRelease, "{0:#,###,##0.00}", new { @id = "usedForLoanRelease_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i + ")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].UsedInWithdrawalOfSavings, "{0:#,###,##0.00}", new { @id = "usedInWithdrawalOfSavings_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i + ")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].Others, "{0:#,###,##0.00}", new { @id = "others_" + i, @class = "single-line", @onchange = "FormatMe(this); GetTotal(" + i + ")", @style = "width:130px" })
                                    </td>
                                    <td style="text-align:right;">
                                        @Html.TextBoxFor(m => m.CollectionReconciliationItems[i].Remarks, new { @class = "single-line", @style = "width:130px" })
                                    </td>
                                    <td>
                                        @Html.Label("", "0.00", new { @id = "lblTotal_" + i } )
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].Total, new { @id = "hdnTotal_" + i })
                                    </td>
                                    <td>
                                        @Html.Label("", "0.00", new { @id = "lblDifference_" + i })
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].Difference, new { @id = "hdnDifference_" + i })

                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].Status)
                                        @Html.HiddenFor(m => m.CollectionReconciliationItems[i].StatusID)
                                    </td>

                                </tr>
                            }
                            </tbody>

                        </table>
                           
                           <button type="submit" id="btnSubmit" class="btn btn-primary" name="command" value=Save>Save</button> 
                           <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>

                    </div>

                </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    @Styles.Render("~/plugins/dataPickerStyles")
    
}

@section Scripts {
@Scripts.Render("~/plugins/dataTables")
@Scripts.Render("~/plugins/dataPicker")

<script type="text/javascript">

    $(document).ready(function () {

        $('.table-List').dataTable({
            ordering: true,
            searching: false,
            paging: false,
            scrollY: 400
        });

        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

    });

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function GetTotal(idx)
    {
        var total = 0;
        total = parseFloat($('#cashDeposited_' + idx).val().replace(',','').replace(',','')) +
                parseFloat($('#deductedToChequeAdvancePayment_' + idx).val().replace(',', '').replace(',', '')) +
                parseFloat($('#deductedToChequeSavings_' + idx).val().replace(',', '').replace(',', '')) +
                parseFloat($('#usedForLoanRelease_' + idx).val().replace(',', '').replace(',', '')) +
                parseFloat($('#usedInWithdrawalOfSavings_' + idx).val().replace(',', '').replace(',', '')) +
                parseFloat($('#others_' + idx).val().replace(',', '').replace(',', ''));

        $('#lblTotal_' + idx).text(FormatNumber(total.toFixed(2)));
        $('#hdnTotal_' + idx).val(FormatNumber(total.toFixed(2)));

        var totalPerRecord = parseFloat($('#hdnTotalPerRecord').val().replace(',', '').replace(',', ''))
        var difference = total - totalPerRecord;

        $('#lblDifference_' + idx).text(FormatNumber(difference.toFixed(2)));
        $('#hdnDifference_' + idx).val(FormatNumber(difference.toFixed(2)));

    }
</script>

}
